package bb.lanxing.lib.devices.ble.device;

import java.util.ArrayList;

import com.bes.bessdk.connect.BleConnector;
import com.bes.sdk.device.HmDevice;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolService;
import com.besall.allbase.view.activity.chipstoollevel4.protocol.ProtocolPresenter;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.util.Log;

import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.ble.BaseBluetoothGattCallback;

public class BesDevice extends AbsBleDevice {
    private static final String TAG = "BesDevice";
    ProtocolPresenter protocolPresenter = null;
    ProtocolService protocolService = null;

    public BesDevice(SmartDevice smartDevice) {
        super(smartDevice);
    }

    public BesDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new BesGattCallback(this));
    }

    @Override
    public boolean isConnected() {
        Log.d(TAG, " BesDevice,isConnected begin");
        /*
         * HmDevice hmDevice = new HmDevice(); SmartDevice smartDevice = getDevice();
         * hmDevice.setDeviceName(smartDevice.getName()); hmDevice.setPreferredProtocol(DeviceProtocol.PROTOCOL_BLE);
         * hmDevice.setBleAddress(smartDevice.getAddress()); hmDevice.setRssi(smartDevice.getRssi()); BesServiceConfig
         * mServiceConfig = new BesServiceConfig(); mServiceConfig.setDeviceProtocol(DeviceProtocol.PROTOCOL_BLE);
         * mServiceConfig.setTotaConnect(false); mServiceConfig.setUseTotaV2(false);
         * mServiceConfig.setServiceUUID(BesSdkConstants.ALIPAY_SERIVICE_UUID);
         * mServiceConfig.setCharacteristicsUUID(BesSdkConstants.ALIPAY_CHARACTERISTICRX_UUID);//
         * ALIPAY_CHARACTERISTICRX_UUID //
         * mServiceConfig.setCharacteristicsTX(BesSdkConstants.ALIPAY_CHARACTERISTICTX_UUID);
         * mServiceConfig.setDescriptorUUID(BES_GATT_DESCRIPTOR_OTA_UUID); mServiceConfig.setDevice(hmDevice);
         * Log.d(TAG, " begin get the bes conn state"); BesSdkConstants.BesConnectState besConnectState =
         * BTService.getDeviceConnectState(mAppContext, mServiceConfig); Log.d(TAG, " the address:" +
         * smartDevice.getAddress() + " ,besConnectState= " + besConnectState); if(besConnectState == BES_CONNECT){
         * return true; }
         */
        if (mAppContext == null) {
            Log.d(TAG, " the context is null,return false");
            return false;
        }
        ArrayList<HmDevice> curConBleDevices = BleConnector.getsConnector(mAppContext, null, null).getCurConnectDevices();
        if (curConBleDevices == null || curConBleDevices.isEmpty()) {
            Log.d(TAG, " the current connected device is null,return false");
            return false;
        }
        HmDevice hmDevice = curConBleDevices.get(0);
        if (hmDevice != null && hmDevice.getBleAddress() != null && hmDevice.getBleAddress().equals(getAddress())) {
            Log.d(TAG, "the device:" + getName() + " is connected,return true");
            return true;
        }

        Log.d(TAG, " BesDevice,return false");
        return false;
        /*
         * protocolPresenter = ProtocolPresenter.getInstance(); if(protocolPresenter != null){ protocolService =
         * protocolPresenter.getProtocolService(); } if(protocolPresenter == null || protocolService == null){
         * Log.d(TAG," null,return false"); return false; } int state = protocolService.getDeviceConnectState();
         * Log.d(TAG,"  state: " + state); if(state != BluetoothProfile.STATE_CONNECTED){
         * Log.d(TAG," not conneccted,return false"); return false; } return true;
         */
    }

    public static class BesGattCallback extends BaseBluetoothGattCallback<BesDevice> {
        public BesGattCallback(BesDevice besDevice) {
            super(besDevice);
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
        }
    }
}
